<?php

namespace App\Http\Controllers\Admin;

use App\Admin\Form;
use App\Admin\Grid;
use App\Admin\Grid\Displayers\Actions;
use App\Admin\Layout\Content;
use App\Admin\Traits\ModelForm;
use App\Models\Department;
use App\Models\Job;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Permission;
use App\Models\Role;

class UserController extends Controller
{
    use ModelForm;

    public function index()
    {
        return \Admin::content(function (Content $content) {
            $content->header('用户');
            $content->description('用户列表');
            $content->body($this->grid()->render());
        });
    }

    protected function grid()
    {
        return User::grid(function (Grid $grid) {
            $grid->id('编号')->sortable();

            $grid->avatar('头像')->image();
            $grid->username('用户名');
            $grid->nickname('昵称');

            $grid->roles('角色')->pluck('display_name')->label();
            $grid->permissions('权限')->pluck('display_name')->label();

            $grid->created_at('添加时间');

            $grid->actions(function (Actions $actions) {
                if ($actions->getKey() == 1) {
                    $actions->disableDelete();
                }
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->is('id', '编号');
                $filter->like('work_no', '工号');
                $filter->like('username', '姓名');
                $filter->like('nickname', '昵称');
            });

            $grid->tools(function (Grid\Tools $tools) {
                $tools->batch(function (Grid\Tools\BatchActions $actions) {
                    $actions->disableDelete();
                });
            });

            $grid->disableExport();
        });
    }

    public function create()
    {
        return \Admin::content(function (Content $content) {
            $content->header('用户');
            $content->description('添加用户');
            $content->row($this->form());
        });
    }

    public function edit($id)
    {
        return \Admin::content(function (Content $content) use ($id) {
            $content->header('用户');
            $content->description('编辑用户');
            $content->row($this->form()->edit($id));
        });
    }

    protected function form()
    {
        return User::form(function (Form $form) {
            $form->display('id', '编号');

            $form->text('username', '姓名');
            $form->text('nickname', '昵称');
            $form->display('union_id', 'Union Id');

            $form->checkbox('roles', '角色')->options(Role::pluck('display_name', 'id')->toArray());
            $form->checkbox('permissions', '权限')->options(Permission::pluck('display_name', 'id')->toArray());

            $form->password('password', '密码');
            $form->password('password_confirmation', '确认密码')->default(function (Form $form) {
                return $form->model()->password;
            });
            $form->ignore(['password_confirmation']);

            $form->display('created_at', '添加时间');
            $form->display('updated_at', '更新时间');

            $form->saving(function (Form $form) {
                if ($form->password && $form->model()->password != $form->password) {
                    $form->password = bcrypt($form->password);
                }
            });
        });
    }
}
